package com.sgb.member.view_model;

import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberClientListBean;
import com.sgb.member.entity.MemberCompanyInfoEntity;
import com.sgb.member.ui.activity.MemberClientManageActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Time:2021/8/26
 * AUTHOR:F_Q
 * Description:
 */
public class ClientManageModel  extends BaseViewModel {
    MemberClientManageActivity activity;
    MemberRepertory repertory;
    private int current;

    public ClientManageModel(MemberClientManageActivity activity, MemberRepertory repertory) {
          this.activity = activity;
          this.repertory = repertory;
    }
    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(activity, "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
        }
    }
    /**
     * 获取列表信息
     * @param isLoadMore
     */
    public void getMemberClientList(boolean isLoadMore) {
        showOrHideDialog(true);
        int currentPage = isLoadMore ? current + 1 : 1;
        Map map = new HashMap();
        map.put("limit","10");
        map.put("page",currentPage);
        repertory.getApi().getMemberClientList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberClientListBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberClientListBean>>> data) {
                if (data.getData() != null ) {
                    current = data.getData().getCurrent();
                    int total = data.getData().getPages();
                    activity.setClientList(data.getData().getList(),current >= total);
                }
            }
        });
    }

    /**
     * 删除数据信息
     * @param no
     */
    public void getMemberClientDelete(String no) {
        showOrHideDialog(true);
        repertory.getApi().getClientDelete(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals("ok")){
                    activity.setMemberClientDelete();
                }
            }
        });
    }
}
